// +----------------------------------------------------------------------
// | CmsWing [ 网站内容管理框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2115 http://www.cmswing.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: arterli <arterli@qq.com>
// +----------------------------------------------------------------------

module.exports = class extends think.Controller {
  /**
   * index action
   * @return {Promise} []
   */
  indexAction() {
    // auto render template file index_index.html
    return this.display();
  }
  async infoexpiredAction() {
    // 订单在规定时间为付款自动作废执行方法
    // 禁止 URL 访问该 Action
    if (!this.isCli) {
      const error = this.controller('cmswing/error');
      return error.noAction('only invoked in cli mode！');
    }

    // 查询上架中 过期的信息
    const dateNow = new Date().getTime();
    const map = {
      status: 5,
      expired_time: ['<', dateNow]
    };
    const info = await this.model('gongxufabu').where(map).field('id').select();
    if (!think.isEmpty(info)) {
      for (const v of info) {
        const remarkData = {};
        remarkData.operation = '下架';
        remarkData.remark_time = dateNow;
        remarkData.remark_name = 1;
        remarkData.foreign_key = v.id;
        remarkData.remark_contents = '信息过期，自动下架';
        remarkData.remark_type = this.ctx.controller + '/' + this.ctx.action;
        remarkData.operation_value = '{"status":6,"problem_type":"信息过期"}';
        remarkData.model_name = 'gongxufabu';
        const remarksId = await this.model('cmswing/remarks').add(remarkData);
        const updateData = {
          offsale_time: dateNow,
          status: 6,
          offsale_remarks_id: remarksId
        };
        await this.model('gongxufabu').where({id: v.id}).update(updateData);
        // 释放库存
      }
    }
    return this.body = '';
  }
  async memberthawAction() {
    // 订单在规定时间为付款自动作废执行方法
    // 禁止 URL 访问该 Action
    if (!this.isCli) {
      const error = this.controller('cmswing/error');
      return error.noAction('only invoked in cli mode！');
    }

    // 查询上架中 过期的信息
    const dateNow = new Date().getTime();
    const map = {
      freeze_status: 1,
      freeze_limit: ['in', [1, 2]]
    };
    const members = await this.model('member').where(map).field('id,locked_time,freeze_limit').select();
    if (!think.isEmpty(members)) {
      for (const v of members) {
        if (dateNow > v.locked_time + get_freezeday(v.freeze_limit) * 86400000) {
          const remarkData = {};
          remarkData.operation = '解冻';
          remarkData.remark_time = dateNow;
          remarkData.remark_name = 1;
          remarkData.foreign_key = v.id;
          remarkData.remark_contents = '冻结时间已到，自动解冻';
          remarkData.remark_type = this.ctx.controller + '/' + this.ctx.action;
          remarkData.model_name = 'member';
          const remarksId = await this.model('cmswing/remarks').add(remarkData);
          const updateData = {
            freeze_status: 0,
            opening_remark_id: remarksId,
            opening_time: dateNow
          };
          await this.model('member').where({id: v.id}).update(updateData);
        }
      }
    }
    return this.body = '';
  }
};
